package com.neusoft.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.mapper.DeliveryAddressMapper;
import com.neusoft.po.DeliveryAddress;
import com.neusoft.service.DeliveryAddressService;
import com.neusoft.util.Lock;

@Service
public class DeliveryAddressServiceImpl implements DeliveryAddressService {

	@Autowired
	private DeliveryAddressMapper deliveryAddressMapper;

	@Override
	public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) {
		return deliveryAddressMapper.listDeliveryAddressByUserId(userId);
	}

	@Override
	public DeliveryAddress getDeliveryAddressById(Integer daId) {
		return deliveryAddressMapper.getDeliveryAddressById(daId);
	}

	@Override
	public int saveDeliveryAddress(DeliveryAddress deliveryAddress) {
		return deliveryAddressMapper.saveDeliveryAddress(deliveryAddress);
	}

	@Override
	@Transactional
	public int updateDeliveryAddress(DeliveryAddress deliveryAddress) {
		// 验证address属于user
		if(Lock.AddressLock(deliveryAddress)) {
			return 0;
		}
		int updateCount = deliveryAddressMapper.updateDeliveryAddress(deliveryAddress);
		if (updateCount == 0) {
			return updateCount;
			// 如果更新操作没有影响任何行，返回更新的行数
		} else {
			// 如果更新操作成功，执行插入操作
			return deliveryAddressMapper.saveDeliveryAddress(deliveryAddress);
		}
	}

	@Override
	public int removeDeliveryAddress(Integer daId) {
		return deliveryAddressMapper.removeDeliveryAddress(daId);
	}
}
